package com.ruoyi.patient.service;


import com.ruoyi.patient.entity.LoginPatient;
import com.ruoyi.patient.entity.MyCartBody;
import com.ruoyi.patient.entity.MyCartReturn;
import com.ruoyi.patient.entity.cart.CartItems;
import com.ruoyi.patient.entity.cart.Datum;
import com.ruoyi.patient.entity.cart.SelectedPkg;
import com.ruoyi.patient.entity.cart.SelectedSpec;
import com.ruoyi.service.domain.*;
import com.ruoyi.service.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Component
public class MyCartService {

    @Autowired
    private ServiceMycartMapper mycartMapper;

    @Autowired
    private ServiceItemscomboMapper itemscomboMapper;

    @Autowired
    private ServiceComboMapper comboMapper;

    @Autowired
    private ServiceItemsMapper itemsMapper;

    @Autowired
    private ServicePackagescomboMapper packagescomboMapper;

    @Autowired
    private ServicePackagesMapper packagesMapper;

    @Autowired
    private ServiceCategoryitemsMapper categoryitemsMapper;

    @Autowired
    private ServiceCategoryMapper categoryMapper;

    @Autowired
    private ServicePackagessuppliesMapper packagessuppliesMapper;

    @Autowired
    private ServiceSuppliesMapper suppliesMapper;

    @Transactional
    public ServiceMycart addMyCart(ServiceMycart oldCart, Long itemId, Long comboId, Long packageId, Long num, LoginPatient oldPatient)
    {
        ServiceMycart newMyCart = new ServiceMycart();
        try {
            if(oldCart == null){
                newMyCart.setMycartUserid(oldPatient.getUserId());
                newMyCart.setMycartItemid(itemId);
                newMyCart.setMycartComboid(comboId);
                newMyCart.setMycartPackageid(packageId);
                newMyCart.setMycartNum(String.valueOf(num));

                ServiceCombo combo = comboMapper.selectServiceComboByComboId(comboId);
                if(packageId == null){
                    newMyCart.setMycartCost(num * (combo.getComboCurrentprice()));
                }else{
                    ServicePackages packages = packagesMapper.selectServicePackagesByPackagesId(packageId);
                    newMyCart.setMycartCost(num * (combo.getComboCurrentprice() + packages.getPackagesPrice()));
                }

                newMyCart.setMycartCreateBy(oldPatient.getUsername());
                newMyCart.setMycartUpdateBy(oldPatient.getUsername());
                newMyCart.setMycartCreateTime(new Date());
                newMyCart.setMycartUpdateTime(new Date());
                mycartMapper.insertServiceMycart(newMyCart);
                return newMyCart;
            }else{
                ServiceCombo combo = comboMapper.selectServiceComboByComboId(comboId);
                Long newNum = Long.valueOf(oldCart.getMycartNum()) + num;
                oldCart.setMycartNum(String.valueOf(newNum));
                if(packageId == null){
                    oldCart.setMycartCost(oldCart.getMycartCost() + num * (combo.getComboCurrentprice()));
                }else{
                    ServicePackages packages = packagesMapper.selectServicePackagesByPackagesId(packageId);
                    oldCart.setMycartCost(oldCart.getMycartCost() + num * (combo.getComboCurrentprice() + packages.getPackagesPrice()));
                }

                oldCart.setMycartUpdateBy(oldPatient.getUsername());
                oldCart.setMycartUpdateTime(new Date());
                mycartMapper.updateServiceMycart(oldCart);
                return oldCart;
            }

        }catch (Exception e){
            return null;
        }
    }


    @Transactional
    public boolean updateMyCart(MyCartBody myCart, LoginPatient oldPatient)
    {
        try {
            ServiceMycart newMyCart = new ServiceMycart();
            newMyCart.setMycartId(myCart.getMyCartId());
            newMyCart.setMycartItemid(myCart.getItemId());
            newMyCart.setMycartNum(myCart.getNum());
            newMyCart.setMycartUpdateBy(oldPatient.getUsername());
            newMyCart.setMycartUpdateTime(new Date());
            mycartMapper.updateServiceMycart(newMyCart);

        }catch (Exception e){
            return false;
        }
        return true;
    }
    @Transactional
    public boolean deleteMyCartByMycartId(Long cartId)
    {
        try {
            mycartMapper.deleteServiceMycartByMycartId(cartId);
        }catch (Exception e){
            return false;
        }
    	return true;
    }

    @Transactional
    public boolean deleteMyCartAll(LoginPatient oldPatient)
    {
        try {
            mycartMapper.deleteServiceMycartAll(oldPatient.getUserId());
        }catch (Exception e){
            return false;
        }
        return true;
    }

    public Datum getMyCartDetail(Long userId)
    {
        Datum retDatum = new Datum();
        Long totalAmount = 0L;
        List<CartItems> ret = new ArrayList<CartItems>();
        List<MyCartReturn> myCartReturn = new ArrayList<MyCartReturn>();
        ServiceMycart searchMyCart = new ServiceMycart();
        searchMyCart.setMycartUserid(userId);
        List<ServiceMycart> mycartList = mycartMapper.selectServiceMycartList(searchMyCart);
        for (ServiceMycart mycart : mycartList) {
            ServiceCombo combo = comboMapper.selectServiceComboByComboId(mycart.getMycartComboid());
            ServiceItemscombo itemscombo = itemscomboMapper.selectByComboId(combo.getComboId());
            ServiceItems item = itemsMapper.selectServiceItemsByItemId(itemscombo.getItemscomboItemid());
            ServiceCategoryitems categoryitems = categoryitemsMapper.selectServiceCategoryitemsByItemsId(item.getItemId());
            ServiceCategory category = categoryMapper.selectServiceCategoryByCategoryId(categoryitems.getCategoryitemsCategoryid());
            ServicePackages packages = packagesMapper.selectServicePackagesByPackagesId(mycart.getMycartPackageid());

            SelectedPkg selectedPkg = null;
            SelectedSpec selectedSpec = null;
            if (packages != null){
                ServicePackagessupplies searchPackagesSupplies = new ServicePackagessupplies();
                searchPackagesSupplies.setPackagessuppliesPackagesid(packages.getPackagesId());
                searchPackagesSupplies.setPackagessuppliesUseFlag("0");
                searchPackagesSupplies.setPackagessuppliesDelFlag("0");
                List<ServicePackagessupplies> packagessuppliesList = packagessuppliesMapper.selectServicePackagessuppliesList(searchPackagesSupplies);
                String pkgDesc = "";
                for (ServicePackagessupplies packagessupplies : packagessuppliesList) {
                    ServiceSupplies supplies = suppliesMapper.selectServiceSuppliesBySuppliesId(packagessupplies.getPackagessuppliesSuppliesid());
                    pkgDesc += supplies.getSuppliesName() + "*" + packagessupplies.getPackagessuppliesCount() + ";";
                }
                selectedPkg = new SelectedPkg(packages);
                selectedPkg.setPkgDesc(pkgDesc);

            }
            selectedSpec = new SelectedSpec(combo);
            MyCartReturn newNode = new MyCartReturn(mycart);

            CartItems retNode = new CartItems(mycart);
            retNode.setCateName(category.getCategoryName());
            retNode.setId(item.getItemId());
            retNode.setImage(item.getItemPicture());
            retNode.setName(item.getItemName());
            retNode.setPrice(combo.getComboCurrentprice());


            retNode.setSelectedPkg(selectedPkg);
            retNode.setSelectedSpec(selectedSpec);

            if (packages != null) {
                totalAmount += combo.getComboCurrentprice()*Long.parseLong(mycart.getMycartNum()) + packages.getPackagesPrice()*Long.parseLong(mycart.getMycartNum());
            }else{
                totalAmount += combo.getComboCurrentprice()*Long.parseLong(mycart.getMycartNum());
            }
            ret.add(retNode);
        }
        retDatum.setCartItems(ret);
        retDatum.setTotalAmount(totalAmount);

    	return retDatum;
    }


}
